import { showModal } from '@/utils/message';
import { getFinance } from '@/api/finance';
import { clientStatusFilter } from '@/filters/status';
import { compareStrings } from '@/utils/string';

export default {
  data() {
    return {
      isSingleWriteOff: true
    };
  },
  methods: {
    btnSubmit(isBatch = false) { // 弹窗提交跳转核销页面
      const { clientName, clientId, clientType: clientFormClientType } = this.$refs.verificationDialog.clientForm;
      const { clientType } = this.writeOffDetail;
      const cFormClientType = clientFormClientType === '个人' ? 1 : 0;
      if (!clientName) {
        this.$message.warning('请填写客户名称');
        return false;
      }
      // 是否可以核销的状态设置
      const writerOffStatus = this.writerOffStatus(clientName, clientType, cFormClientType);
      // 跳转到核销页面前的判断
      this.toAddPaymanPage(clientId, clientName, writerOffStatus, isBatch);
    },
    checkVerificate(isSingleWriteOff = true, ids) { // 点击核销按钮验证
      this.writeOffDetail = {};
      if (this.$refs.verificationDialog) {
        this.$refs.verificationDialog.clientName = [];
        this.$refs.verificationDialog.clientNameList();
        this.$refs.verificationDialog.clientForm = {
          clientName: '',
          clientId: '',
          clientType: '',
          clientTypeId: ''
        };
      }
      const params = Object.create(null);
      params.ids = ids;
      params.writeOffType = isSingleWriteOff ? 0 : 1;
      this.isSingleWriteOff = isSingleWriteOff;
      getFinance(params).then(res => { // 获取当前该条流水单信息
        this.writeOffDialog = true;
        this.writeOffDetail = res.data;
        if (this.writeOffDetail.clientId) { // 判断客户ID是否存在，如果存在的话则将信息中的客户名称回填到输入框中
          const obj = {
            clientId: this.writeOffDetail.clientId,
            clientName: this.writeOffDetail.payName,
            clientType: clientStatusFilter(this.writeOffDetail.bookedClientType)
          };
          this.clientFormDefalut = obj;
        }
      });
    },
    toTurnPage(clientId, clientName, routerName = 'chargeOff') {
      if (clientId) {
        this.writeOffDialog = false;
        const params = {
          dayAccountId: this.flowId || this.dayAccountId,
          customerId: clientId,
          clientId
        };
        const query = routerName === 'chargeOff' ? { clientName, verificationState: this.activeId || this.activeId === 0 ? this.activeId : this.detailInfo.verificationState }
          : { activeTab: 'payer', routeName: 'detailCustomer' };
        query.clientName = encodeURI(query.clientName);
        const routeUrl = this.$router.resolve({
          name: routerName,
          params,
          query
        });
        window.open(routeUrl.href, '_blank');
      } else {
        this.$message.error('付款人名称不存在');
      }
    },
    // 客户名称 付款人类型 客户类型
    writerOffStatus(clientName, clientType, clientFormClientType) {
      const { payer = [] } = this.$refs.verificationDialog.clientForm;
      const { payName, payNumber } = this.writeOffDetail;
      /* 详见v2.9.1需求文档
       * 1 可核销：
       * 付款名称等于客户名称并且付款账户类型等于客户类型
       * 付款名称不等于客户名称并且付款账户类型不等于客户类型
       * 客户名称等于个人并且付款账户类型和客户类型都为个人（个人可为公司代付）
       * 2 添加付款人后可核销:
       * 付款名称不等于客户名称并且付款账户类型等于客户类型
       * 并且客户付款人列表里不存在付款人名称和付款账号和流水一一对应的付款人
       * 0 不可核销 情况1 付款类型与客户类型不一致
       * 3 不可核销 情况2 付款名称不等于客户名称并且付款账户类型不等于客户类型（公司不可为个人代付）
      */
      const compareResult = compareStrings(payName, clientName);
      if (!compareResult && clientType === 0 && clientFormClientType === 1) {
        return 3;
      }
      if ((compareResult && clientType === clientFormClientType) ||
      (!compareResult && clientType !== clientFormClientType) ||
      (['个人', '个人-达人'].includes(clientName) && clientType === 1 && clientFormClientType === 1)) {
        return 1;
      }
      if (!compareResult && clientType === clientFormClientType) {
        // 付款人状态：0--禁用；1--启用
        return payer !== null && payer.find(i => compareStrings(i.name, payName) && i.account === payNumber && i.status === 1) ? 1 : 2;
      }
      return 0;
    },
    toAddPaymanPage(clientId, clientName, writerOffStatus, isBatch) {
      const confirmMsgArr = [
        {
          type: 0,
          msg: '客户类型与付款类型不一致，无法核销'
        },
        {
          type: 1
        },
        {
          type: 2,
          // msg: '流水付款人名称与该客户名称不一致，可前往该客户详情添加与该流水对应付款人+付款账号，即可核销该流水',
          msg: '流水付款人名称与该客户名称不一致，请联系财务前往客户详情添加付款人，再进行核销。',
          btnText: '前往添加付款人',
          toCustomer: true
        },
        {
          type: 3,
          msg: '付款类型为公司的流水无法核销客户类型为个人的订单'
        }
      ];
      const { type, msg, btnText, toCustomer } = confirmMsgArr.find(i => i.type === writerOffStatus);
      const showConfirm = type !== 1;
      if (!showConfirm) {
        if (isBatch) {
          this.writeOffDialog = false;
          const encodeclientName = encodeURI(clientName);
          this.$router.push({
            name: 'batchWriteOff',
            query: {
              flowId: this.ids,
              clientId,
              clientName: encodeclientName
            }
          });
        } else {
          this.toTurnPage(clientId, clientName, 'chargeOff');
        }
        return false;
      }
      showModal(msg, '', {
        confirmButtonText: btnText || '确定',
        lockScroll: false,
        customClass: 'del-message del-message-cancel charge-message-cancel',
        iconClass: 'del-icon'
      }).then(() => {
        if (toCustomer) {
          this.toTurnPage(clientId, clientName, 'editCustomer');
        }
      }).catch(() => {
        return false;
      });
    }
  }
};
